public class ctrl_AmendmentActivities
{
    //For Variables...
    //For Payment Generation Variables...
    Public List<ReceiptWrapper> ReceiptWrapperList {get; set;}
    public Amendment_Activity__c NewPayment {get; set;}
    public Boolean actionbuttonADD {get; set;}
    public Boolean actionbuttonEdit {get; set;}
    public string RowIndex {get; set;}
    public string RowIndex2 {get; set;}
    public string ReceiptID {get; set;}
    public string receiptName {get;set;}
    public string receiptAccountId {get;set;}
    public Boolean refreshPage {get;set;}
    Public Boolean delButtonFlag {get;set;}
    Public Boolean addButtonFlag {get;set;}
    Public Boolean editButtonFlag {get;set;}
    
    //For Contructors...
    public ctrl_AmendmentActivities(ApexPages.StandardController controller) 
    {
         refreshPage = false;
         
         //Flag To Control the Trigger...
         delButtonFlag = true;
         addButtonFlag = true;
         actionbuttonADD = true;
         editButtonFlag = true;
    }
    
    //For Generating the Payment Module Initiator Method....
    public PageReference generatePaymentModule()
    {
        List<Amendment__c> Receipts = [SELECT id, Name,Account__c,New_License_Type__c,Total_Active_Amendment_Activities__c,Record_Type_Name_Formula__c,Action__c FROM Amendment__c WHERE Id = :ApexPages.CurrentPage().getParameters().get('id')];
        NewPayment = new Amendment_Activity__c();
        
        ReceiptWrapperList = new List<ReceiptWrapper>();
        
        For(Amendment__c Receipts_trav : Receipts)
        {
            receiptName = Receipts_trav.name;
            NewPayment.Amendment__c = Receipts_trav.id;
            receiptAccountId = Receipts_trav.Account__c;
            //ReceiptWrapperList.Add(new ReceiptWrapper(Receipts_trav.name, Receipts_trav.Receipt_Type__c, Receipts_trav.id));
            ReceiptWrapperList.Add(new ReceiptWrapper(Receipts_trav.name, Receipts_trav.Record_Type_Name_Formula__c, Receipts_trav.Total_Active_Amendment_Activities__c, Receipts_trav.id));
        }
        
        for(ReceiptWrapper ReceiptWrapper_trav : ReceiptWrapperList)
        {
            try
            {
                ReceiptWrapper_trav.Payments = [SELECT id,name,Account__c,Active__c,Add_Flag__c,Edit_Flag__c,Delete_Flag__c,Activity_Arabic_Name__c,Activity_English_Name__c,Amendment__c,Activity__c FROM Amendment_Activity__c where Amendment__c = :ReceiptWrapper_trav.RecordId AND Active__c =: true AND Delete_Flag__c =: false];
                System.debug('The Total Number of Amendment Activities in Wrapper class is => '+ ReceiptWrapper_trav.Payments.size());       
            }
            catch(System.Exception er)
            {
                ReceiptWrapper_trav.Payments = new List<Amendment_Activity__c>();
            }
        }
        
        
        CreatedDate = System.Today().format();
        actionbuttonADD = true;
        actionbuttonEdit = false;
        
    
        return null;   
    }
    
    //Helping class and Methods for the Payment Module....
    /*public List<string> getTableHeaders()
    {
        List<string> str = new List<string>();
        
        str.add('Shareholders');
        str.add('Share Holder Ownership %');
        str.add('Share Holder Profit %');
        //str.add('Method');
        //str.add('Amount');
        
        return str;
    }*/
     
    public string CreatedDate {get; set;}
    
    Public Boolean InitializerFlag = true;
    //System.debug('The InitializerFlag Value Before => '+ InitializerFlag );
    public string check1 {get; set;}
    public PageReference additems()
    {
        check1 = 'entered';
        NewPayment.Amendment__c = ApexPages.CurrentPage().getParameters().get('id');
        
        try
        {
            Decimal BalanceCheck = 0.0;
            for(ReceiptWrapper ReceiptWrapper_trav : ReceiptWrapperList)
            {
                System.debug('The Receipt Wrapper Id => '+ ReceiptWrapper_trav.RecordID);
                check1 = ReceiptWrapper_trav.RecordID;
                System.Debug(ReceiptWrapper_trav.RecordID + ' - ' + NewPayment.Amendment__c);
                check1 = ReceiptWrapper_trav.RecordID + '-' + NewPayment.Amendment__c;
                if(ReceiptWrapper_trav.RecordID == NewPayment.Amendment__c)
                {
                    check1 = 'testing';
                   /*if(ReceiptWrapper_trav.ReceiptBalance < 0)
                    {
                        ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.INFO, 'Provided amount is more than balance amount');
                        ApexPages.addMessage(msg);
                        break;
                    }
                    else if(ReceiptWrapper_trav.Payments.size() < 12)*/
                    if(ReceiptWrapper_trav.Payments.size() < 12)
                    {
                        System.Debug('Before Insert the Payment');
                        NewPayment.Add_Flag__c = true;
                        NewPayment.Active__c = true;
                        insert(NewPayment);
                        check1 = NewPayment.Id;
                        NewPayment = new Amendment_Activity__c();
                        
                        //For Page Refreshing...
                        refreshPage = true;
                    }
                    else
                    {
                        ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.INFO, 'More than twelve(12) Shareholderes are not allowed for Amendment #: ' + ReceiptWrapper_trav.ReceiptNumber + '. Please adjust the balance amount in existing payments.');
                        ApexPages.addMessage(msg);
                        break;
                    }
                }
            }
            for(ReceiptWrapper ReceiptWrapper_trav : ReceiptWrapperList)
            {
                try
                {
                    //ReceiptWrapper_trav.Payments = [SELECT id, name,Ownership__c,Add_Flag__c,Edit_Flag__c,Delete_Flag__c, ShareHolder_Name__c,Share_Ownership__c, Share_Profit__c,Contact__c, Amendment__c,Account__c FROM Amendment_Activity__c WHERE Amendment__c = :ReceiptWrapper_trav.RecordId AND Delete_Flag__c =: false];
                    ReceiptWrapper_trav.Payments = [SELECT id,name,Account__c,Active__c,Add_Flag__c,Edit_Flag__c,Delete_Flag__c,Activity_Arabic_Name__c,Activity_English_Name__c,Amendment__c,Activity__c FROM Amendment_Activity__c where Amendment__c = :ReceiptWrapper_trav.RecordId AND Active__c =: true AND Delete_Flag__c =: false];
                    
                    
                }
                catch(System.Exception er)
                {
                    ReceiptWrapper_trav.Payments = new List<Amendment_Activity__c>();
                }
            }
            return null;
        }
        catch(System.Exception er)
        {
            ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.INFO, er.getMessage() + ' - Line Number: ' + er.getlinenumber());
            ApexPages.addMessage(msg);
            return null;
        }
    }
    
    public void EditAllow()
    {
        System.debug('The RowIndex Value => '+ RowIndex);
        System.debug('The Last Index Of ":" => '+ RowIndex.lastIndexof(':'));
        System.debug('The Last Index Of "J" => '+ RowIndex.lastIndexof('j'));
        string valindex = RowIndex.substring(RowIndex.lastIndexof(':') - 1, RowIndex.lastIndexof(':'));
        string val2index = RowIndex.substring(RowIndex.lastIndexof('j') - 2, RowIndex.lastIndexof('j') - 1);
        RowIndex = valindex;
        
        System.debug('The val1Index => '+ valindex);
        System.debug('The val2Index => j_id0:MainForm:j_id50:0:j_id52:0:EDITID'+ val2index);
        
        integer indexA = integer.valueof(valindex);
        integer indexB = integer.valueof(val2index);
        
        System.debug('The Receipt Wrapper Payment List => '+ ReceiptWrapperList[indexB].Payments.size() + ' && The Index A =>'+ indexA + ' && The Index B => '+ indexB);
        NewPayment = new Amendment_Activity__c();
        NewPayment = ReceiptWrapperList[indexB].Payments.get(indexA);
        System.debug('The New Payment with wrapper Class');
        actionbuttonADD = false;
        actionbuttonEdit = true;
    }
    
    public void DelAllow()
    {
        string valindex = RowIndex.substring(RowIndex.lastIndexof(':') - 1, RowIndex.lastIndexof(':'));
        
        System.debug('The Val Index 1 => '+ valindex );
        System.debug('The Last Index Of ":" => '+ RowIndex.lastIndexof(':'));
        System.debug('The Last Index Of "J" => '+ RowIndex.lastIndexof('j'));
        
        string val2index = RowIndex.substring(RowIndex.lastIndexof('j') - 2, RowIndex.lastIndexof('j') - 1);
        System.debug('The Val Index 2 => '+ val2index );
        
        
        RowIndex = valindex;
        
        integer indexA = integer.valueof(valindex);
        integer indexB = integer.valueof(val2index);
        Amendment_Activity__c PT = ReceiptWrapperList[indexB].Payments.get(indexA);
        
        try
        {
            PT.Edit_Flag__c = false;
            PT.Delete_Flag__c = true;
            System.debug('Before Update Delete Flag => ' + PT.Delete_Flag__c);
            update(PT);
            //For Page Refreshing...
            refreshPage = true;
                
            System.debug('After Update Delete Flag => ' + PT.Delete_Flag__c);
            //delete(PT);
            NewPayment = new Amendment_Activity__c();
            for(ReceiptWrapper ReceiptWrapper_trav : ReceiptWrapperList)
            {
                try
                {
                    //ReceiptWrapper_trav.Payments = [SELECT id, Add_Flag__c,Edit_Flag__c,Delete_Flag__c,name,Ownership__c, ShareHolder_Name__c,Share_Ownership__c, Share_Profit__c,Contact__c, Amendment__c,Account__c FROM Amendment_Activity__c WHERE Amendment__c = :ReceiptWrapper_trav.RecordId AND Delete_Flag__c =: false];
                    
                    ReceiptWrapper_trav.Payments = [SELECT id,name,Account__c,Active__c,Add_Flag__c,Edit_Flag__c,Delete_Flag__c,Activity_Arabic_Name__c,Activity_English_Name__c,Amendment__c,Activity__c FROM Amendment_Activity__c where Amendment__c = :ReceiptWrapper_trav.RecordId AND Active__c =: true AND Delete_Flag__c =: false]; 
                    
                }
                catch(System.Exception er)
                {
                    ReceiptWrapper_trav.Payments = new List<Amendment_Activity__c>();
                }
            }
        }
        catch(System.Exception er)
        {
            ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.INFO, er.getMessage());
            ApexPages.addMessage(msg);
        }
        
        actionbuttonADD = true;
        actionbuttonEdit = false;
    }
    
    public string updateitemlist()
    {
        try
        {
            for(ReceiptWrapper ReceiptWrapper_trav : ReceiptWrapperList)
            {
                check1 = ReceiptWrapper_trav.RecordID;
                System.Debug(ReceiptWrapper_trav.RecordID + ' - ' + NewPayment.Amendment__c);
                Decimal BalanceCheck = 0;
                if(ReceiptWrapper_trav.RecordID == NewPayment.Amendment__c)
                {
                    /*if(BalanceCheck < 0)
                    {
                        ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.INFO, 'Provided amount is more than balance amount');
                        ApexPages.addMessage(msg);
                    }
                    else
                    {*/
                        NewPayment.Edit_Flag__c = true;
                        NewPayment.Delete_Flag__c = false;
                        update(NewPayment);
                        //For Page Refreshing...
                        refreshPage = true;
                        for(ReceiptWrapper ReceiptWrapper_trav1 : ReceiptWrapperList)
                        {
                            try
                            {
                                //ReceiptWrapper_trav1.Payments = [SELECT id, Add_Flag__c,Edit_Flag__c,Delete_Flag__c,name,Ownership__c, ShareHolder_Name__c,Share_Ownership__c, Share_Profit__c,Contact__c, Amendment__c,Account__c FROM Amendment_Activity__c WHERE Amendment__c = :ReceiptWrapper_trav1.RecordId AND Delete_Flag__c =: false];
                                //ReceiptWrapper_trav1.ReceiptBalance = ReceiptWrapper_trav1.ReceiptAmount;
                                ReceiptWrapper_trav.Payments = [SELECT id,name,Account__c,Active__c,Add_Flag__c,Edit_Flag__c,Delete_Flag__c,Activity_Arabic_Name__c,Activity_English_Name__c,Amendment__c,Activity__c FROM Amendment_Activity__c where Amendment__c = :ReceiptWrapper_trav.RecordId AND Active__c =: true AND Delete_Flag__c =: false];
                            }
                            catch(System.Exception er)
                            {
                                ReceiptWrapper_trav1.Payments = new List<Amendment_Activity__c>();
                            }
                        }
                        NewPayment = new Amendment_Activity__c();
                        actionbuttonADD = true;
                        actionbuttonEdit = false;
                    //}
                    break;
                }
            }
        }
        catch(System.Exception er)
        {
            ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.INFO, er.getMessage());
            ApexPages.addMessage(msg);
        }

        return null;
    }
    /*public List<SelectOption> getReceiptList()
    {
        List<SelectOption> options = new List<SelectOption>();
        
        List<Amendment__c> Receipts = [SELECT id, name FROM Amendment__c WHERE Id =:ApexPages.CurrentPage().getParameters().get('id')];
        options.add(new SelectOption('', '-- SELECT RECEIPT --'));
        for(Amendment__c Receipt_trav : Receipts)
        {
            options.add(new SelectOption(Receipt_trav.id, Receipt_trav.name));
        }
        
        return options;
    }*/
    
    public void cancelEdit()
    {
        try
        {
            NewPayment = new Amendment_Activity__c();
            for(ReceiptWrapper ReceiptWrapper_trav : ReceiptWrapperList)
            {
                try
                {
                    //ReceiptWrapper_trav.Payments = [SELECT id, Add_Flag__c,Edit_Flag__c,Delete_Flag__c,name,Ownership__c, ShareHolder_Name__c,Share_Ownership__c, Share_Profit__c,Contact__c, Amendment__c,Account__c FROM Amendment_Activity__c WHERE Amendment__c = :ReceiptWrapper_trav.RecordId AND Delete_Flag__c =: false];
                    ReceiptWrapper_trav.Payments = [SELECT id,name,Account__c,Active__c,Add_Flag__c,Edit_Flag__c,Delete_Flag__c,Activity_Arabic_Name__c,Activity_English_Name__c,Amendment__c,Activity__c FROM Amendment_Activity__c where Amendment__c = :ReceiptWrapper_trav.RecordId AND Active__c =: true AND Delete_Flag__c =: false];
                }
                catch(System.Exception er)
                {
                    ReceiptWrapper_trav.Payments = new List<Amendment_Activity__c>();
                }
            }
        }
        catch(System.Exception er)
        {
            ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.INFO, er.getMessage());
            ApexPages.addMessage(msg);
        }
        actionbuttonADD = true;
        actionbuttonEdit = false;
    }
    

    //Receipt Wrapper Class..
    class ReceiptWrapper
    {
        Public String ReceiptNumber {get; set;}
        Public String ReceiptGroup {get; set;}
        Public Decimal ReceiptAmount {get; set;}
        Public Decimal ReceiptBalance {get; set;}
        Public ID RecordId {get; set;}
        
        Public List<Amendment_Activity__c> Payments {get; set;}
        
        public ReceiptWrapper(String RNumber, String RGroup,Decimal RAmount, ID RID)
        {
            ReceiptNumber = RNumber;
            ReceiptGroup = RGroup;
            ReceiptAmount = RAmount;
            ReceiptBalance = RAmount;
            RecordId = RID;
        }
        
    }



}